package no.ffi.gismoidm.pubsub;

import java.util.Iterator;
import java.util.List;

/**
 * Representation of a subscription topic as a hierarchical entity. Its string format are names
 * separated by "/"
 * @author anders
 */
public class SubscriptionTopic implements java.io.Serializable {
    String topicString;
    public SubscriptionTopic(String topic) { 
        if (!topic.endsWith("/")) topic = topic + "/";
        topicString = topic; 
    }
    
    public String toString() { return topicString; }
    
    /**
     * Match subscription topic (ST) to publication topic (PT)
     * PT=media/news/Norway matches ST=media/news
     * but not the other way around
     */
    public boolean matchTopics(List publicationTopics) {
        Iterator<String> it2 = publicationTopics.iterator();
        while (it2.hasNext()) {
            String s2 = it2.next();
            if (!s2.endsWith("/")) s2 = s2 + "/";
            if (s2.startsWith(topicString)) return true;
        }
        return false;
    }
    
}
